package com.yuanian.timingwheel.infrastructure;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取当天0点时间
     * @return
     */
    public static long zeroDay() {
        return zeroDay(new Date());
    }

    /**
     * 获取当天0点时间
     * @return
     */
    public static long zeroDay(Date date) {
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            return dayFormat.parse(dayFormat.format(date)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 当前还剩下多少秒
     * @return
     */
    public static long dayRestSecond() {
        return dayRestSecond(new Date());
    }

    /**
     * 当天还剩下多少毫秒
     * @return
     */
    public static long dayRestSecond(Date date) {
        return 24 * 60 * 60 * 1000 - (date.getTime() - zeroDay());
    }

    /**
     * 已经流失的秒总数,包含0点0分0秒
     * @param date
     * @return
     */
    public static long dayPassSecond(Date date, Date date1) {
        long num = (Long)(date.getTime() - zeroDay(date1)) / 1000;
        return  num;
    }

    public static int dayPassSecondShort(Date date, Date date1) {
        return  (int)dayPassSecond(date, date1);
    }

    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static int getSecond() {
        return Calendar.getInstance().get(Calendar.SECOND);
    }

    public static Date getNextSecond(Date now){
        return new Date(now.getTime() + 1000);
    }

    public static long restSecond() {
        return 1000 - System.currentTimeMillis() % 1000;
    }

    public static void main(String[] args) {
        //while (true) {
        Date d = new Date();
            System.out.println(new Date(zeroDay(new Date())));
            System.out.println(restSecond());
        //}

    }

}

